/**
 * @Title: PayFeeController.java
 * @Package com.qding.park.api.web.service.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2011 
 * Company:千丁互联
 * 
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:48:51
 * @version V1.0
 */

package com.qding.park.manager.rest.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.business.adapter.AdapterException;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.core.exceptions.business.validator.RchlValidatorException;
import com.qding.park.common.core.validate.RchlValidateBean;
import com.qding.park.common.core.validate.RchlValidateTools;
import com.qding.park.manager.common.pojo.ParkDataResult;
import com.qding.park.manager.common.rest.domain.AnswerMsg;
import com.qding.park.manager.common.rest.domain.AnswerMsgFactoryImpl;
import com.qding.park.manager.common.rest.enums.ResponseCodeEnum;
import com.qding.park.manager.facade.core.service.DemoFacade;
import com.qding.park.manager.facade.core.service.common.PayCommonFacade;
import com.qding.park.manager.facade.core.service.enums.TempPaymentEnum;
import com.qding.park.manager.pojo.enums.OrderServiceType;
import com.qding.park.manager.pojo.payment.OrderParameter;
import com.qding.park.manager.pojo.payment.PayResultEnum;
import com.qding.park.manager.rest.conf.SystemConfig;
import com.qding.park.manager.rest.enums.OrderTypeVoEnum;

/**
 * @ClassName: PayFeeController
 * @Description: 缴费Controller
 * @author Comsys-Administrator
 * @date 2016年8月16日 下午2:48:51
 *
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/payFee")
public class PayFeeController extends WebBaseController {

	private static final Log _log = LogFactory.getLog(PayFeeController.class);

//	@Autowired
//	private ParkFacade parkFacade;

//	@Autowired
//	private PayFeeFacade payFeeFacade;

//	@Autowired
//	private PayFeeStatisFacade payFeeStatisFacade;

	@Autowired
	private SystemConfig systemConfig;

	@Autowired
	private DemoFacade demoFacade;
	
	@Autowired
	private  PayCommonFacade   payCommonFacade;

	/**
	 * @Description (创建缴费单)
	 * @param parkId
	 * @param carNum
	 * @param userId
	 * @param orderType
	 * @return
	 */
	@RequestMapping("/createPayment")
	public AnswerMsg createPayment(String parkId, String carNum, String userId, String orderType) {
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			// vb.getRequiredValidate().put("parkId", parkId);
			vb.getRequiredValidate().put("carNum", carNum);
			vb.getRequiredValidate().put("userId", userId);
			vb.getRequiredValidate().put("orderType", orderType);

			// vb.getIntValidate().put("parkId", parkId);
			vb.getIntValidate().put("orderType", orderType);
			RchlValidateTools.validate(vb);
			
			vb.getRequiredValidate().clear();
			vb.getIntValidate().clear();
			int orderTypeInt = Integer.valueOf(orderType);
			int parkIdInt = 0;
			if (orderTypeInt == OrderTypeVoEnum.MONTHRENT_PAY.getOrderType()
					|| orderTypeInt == OrderTypeVoEnum.TEMP_PAY.getOrderType()) {
				vb.getRequiredValidate().put("parkId", parkId);
				vb.getIntValidate().put("parkId", parkId);
				RchlValidateTools.validate(vb);
				parkIdInt = Integer.valueOf(parkId);
			}
			/**************/

			ParkDataResult parkDataResult = null;
			if (isDemoRequest(userId)) {
				// 演示请求
				if (orderTypeInt == OrderTypeVoEnum.TENERAT_PAY.getOrderType()) {
					// 代缴费用
					parkDataResult = demoFacade.createTeneratPayment(parkIdInt, carNum, userId);
					AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
					return answerMsg;
				} else if (orderTypeInt == OrderTypeVoEnum.MONTHRENT_PAY.getOrderType()) {
					// 月租
					parkDataResult = demoFacade.createmonthPayment_Mon(parkIdInt, carNum, userId);
					AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
					return answerMsg;
				} else if (orderTypeInt == OrderTypeVoEnum.TEMP_PAY.getOrderType()) {
					// 临停缴费
					parkDataResult = demoFacade.createTempPayment_Temp(parkIdInt, carNum, userId);
					AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
					return answerMsg;
				} else {
					AnswerMsg answerMsg = AnswerMsgFactoryImpl
							.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(), "非法的缴费类型");
					return answerMsg;
				}
			} else {
				if (orderTypeInt == OrderTypeVoEnum.TENERAT_PAY.getOrderType()) {
					// 代缴费用
				    payCommonFacade.createTempPayment(parkIdInt, carNum, userId, TempPaymentEnum.TENERAT);
					AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
					return answerMsg;
				} else if (orderTypeInt == OrderTypeVoEnum.MONTHRENT_PAY.getOrderType()) {
					// 月租
				    parkDataResult= payCommonFacade.createmonthPayment(parkIdInt, carNum, userId);
					AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
					return answerMsg;
				} else if (orderTypeInt == OrderTypeVoEnum.TEMP_PAY.getOrderType()) {
					// 临停缴费
				    parkDataResult = payCommonFacade.createTempPayment(parkIdInt, carNum, userId,
				            TempPaymentEnum.TEMP);
					AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
					return answerMsg;
				} else {
					AnswerMsg answerMsg = AnswerMsgFactoryImpl
							.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(), "非法的缴费类型");
					return answerMsg;
				}
			}

		} catch (RchlValidatorException e) {
			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (AdapterException e) {
			exceptionHandle.handle(e);// 接口异常
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (BaseException e) {
			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			_log.error(e, e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR.getValue(),
					"系统异常，请稍后再试");
			return answerMsg;
		}

	}

	/**
	 * @Description (计算月租金)
	 * @param orderId
	 * @param monthNum
	 * @return
	 */
	@RequestMapping("/calMonthFee")
	public AnswerMsg calMonthFee(String orderId, String monthNum) {
		
		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderId", orderId);
			vb.getRequiredValidate().put("monthNum", monthNum);

			// vb.getIntValidate().put("orderId", orderId);
			vb.getIntValidate().put("monthNum", monthNum);
			RchlValidateTools.validate(vb);
			/**************/

			ParkDataResult parkDataResult =null;
			if (orderId.endsWith(OrderServiceType.DEMO_ORDER.getTag())) {
				//演示请求
				parkDataResult=demoFacade.calMonthFee(orderId,Integer.valueOf(monthNum));
			}else{
				 parkDataResult = payCommonFacade.calMonthFee(orderId, Integer.valueOf(monthNum));
			}
			
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (ServiceCoreException e) {
			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getName());
			return answerMsg;
		} catch (RchlValidatorException e) {

			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (AdapterException e) {
			exceptionHandle.handle(e);// 接口异常
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (BaseException e) {
			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR.getValue(),
					"系统异常，请稍后再试");
			return answerMsg;
		}

	}

	/**
	 * payNotice(创建支付单单)
	 *
	 * @Title: createPayOrder @Description: TODO @param @param
	 * orderId @param @param orderType @param @param payStatus @param @param
	 * monthNum @param @return 设定文件 @return AnswerMsg 返回类型 @throws
	 */
	@RequestMapping("/createPayOrder")
	public AnswerMsg createPayOrder(String orderId, String orderType) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("orderType", orderType);
			vb.getRequiredValidate().put("orderId", orderId);

			vb.getIntValidate().put("orderType", orderType);
			RchlValidateTools.validate(vb);

			int orderTypeInt = Integer.valueOf(orderType);
			// vb.getRequiredValidate().clear();
			// if(orderTypeInt!=OrderTypeVoEnum.RESERVE_PAY.getOrderType()){
			// vb.getRequiredValidate().put("orderId", orderId);
			// }
			/**************/

			ParkDataResult parkDataResult = null;
			if (orderId.endsWith(OrderServiceType.DEMO_ORDER.getTag())) {
				// 演示数据
				String bjproject=systemConfig.calculationDemoProject().get(0);
				if (OrderTypeVoEnum.TEMP_PAY.getOrderType() == orderTypeInt) {
					// 临停缴费
					parkDataResult = demoFacade.createTemPayOrder_Temp(orderId,bjproject);
				} else if (orderTypeInt == OrderTypeVoEnum.TENERAT_PAY.getOrderType()) {
					// 代缴费
					parkDataResult = demoFacade.createTemPayOrder_Temp(orderId,bjproject);
				} else if (orderTypeInt == OrderTypeVoEnum.RESERVE_PAY.getOrderType()) {
					// 预定
					// parkDataResult=payFeeFacade.createReservePayOrder_rest();
				} else if (OrderTypeVoEnum.MONTHRENT_PAY.getOrderType() == orderTypeInt) {
					// 月租续费
					parkDataResult = demoFacade.createMonthPayOrder_Mon(orderId,bjproject);
				}
			} else {

				if (OrderTypeVoEnum.TEMP_PAY.getOrderType() == orderTypeInt) {
					// 临停缴费
					parkDataResult = payCommonFacade.createTemPayOrder(orderId);
				} else if (orderTypeInt == OrderTypeVoEnum.TENERAT_PAY.getOrderType()) {
					// 代缴费
					parkDataResult = payCommonFacade.createTeneratPayOrder(orderId);
				} else if (orderTypeInt == OrderTypeVoEnum.RESERVE_PAY.getOrderType()) {
					// 预定
					// parkDataResult=payFeeFacade.createReservePayOrder_rest();
				} else if (OrderTypeVoEnum.MONTHRENT_PAY.getOrderType() == orderTypeInt) {
					// 月租续费
					parkDataResult = payCommonFacade.createMonthPayOrder(orderId);
				}
			}

			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (BaseException e) {
			exceptionHandle.handle(e);// 接口异常
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR.getValue(),
					"系统异常，请稍后再试");
			return answerMsg;
		}
	}


	/**
	 * payResultNotice(支付结果通知)
	 *
	 * @Title: payResultNotice @Description: TODO @param @param szOrderId
	 * 深圳订单号 @param @param orderType 订单类型 @param @param payResult
	 * 支付结果 @param @param resultDescription 支付结果描述 @param @param realPayAmount
	 * 实际支付金额 @param @return 设定文件 @return AnswerMsg 返回类型 @throws
	 */
	@RequestMapping("/payResultNotice")
	public AnswerMsg payResultNotice(String szOrderId, String orderType, String payResult, String resultDescription,
			String realPayAmount) {

		try {
			/****** 验证字段 ********/
			RchlValidateBean vb = new RchlValidateBean();
			vb.getRequiredValidate().put("szOrderId", szOrderId);
			vb.getRequiredValidate().put("orderType", orderType);
			vb.getRequiredValidate().put("payResult", payResult);

			if (!StringUtils.isBlank(realPayAmount)) {
				vb.getIntValidate().put("realPayAmount", realPayAmount);
			}
			vb.getIntValidate().put("orderType", orderType);
			vb.getIntValidate().put("payResult", payResult);
			RchlValidateTools.validate(vb);
			/**************/

			// int orderIdInt=Integer.valueOf(orderId);
			int orderTypeInt = Integer.valueOf(orderType);
			int payResultInt = Integer.valueOf(payResult);

			int realPayAmountInt = -10000;
			if (!StringUtils.isBlank(realPayAmount)) {
				realPayAmountInt = Integer.valueOf(realPayAmount);
			}

			// if(payResultInt==PayResultEnum.FAIL.getCode()){
			// //支付失败
			// return
			// AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
			// ");
			// }
			if(this.isDemoRequestByOrder(szOrderId)){
				//演示请求
				return AnswerMsgFactoryImpl.createAnswerMsg(ParkDataResult.createEmpty().getData());
			}
			
			ParkDataResult parkDataResult = null;
			if (payResultInt == PayResultEnum.SUCCESS.getCode()) {
				// 支付成功
				if (orderTypeInt == OrderParameter.OrderTypeEnum.MONTH_PAY_TYPE.getType()) {
					// 月租
					parkDataResult = payCommonFacade.monthPaySuccessNotice(szOrderId, realPayAmountInt);
				} else if (orderTypeInt == OrderParameter.OrderTypeEnum.CUSTOMER_PAYMENT_TYPE.getType()) {
					// 代缴
					parkDataResult = payCommonFacade.teneratPaySuccessNotice(szOrderId, realPayAmountInt);
				} else if (orderTypeInt == OrderParameter.OrderTypeEnum.RESERVER_TYPE.getType()) {
					// 预定支付成功
					parkDataResult = payCommonFacade.reserverPaySuccessNotice(szOrderId, realPayAmountInt);
				} else if (orderTypeInt == OrderParameter.OrderTypeEnum.TEMP_PAY_TYPE.getType()) {
					// 临停
					parkDataResult = payCommonFacade.tempPaySuccessNotice(szOrderId, realPayAmountInt);
				}

			} else {
				// 支付失败

				if (orderTypeInt == OrderParameter.OrderTypeEnum.MONTH_PAY_TYPE.getType()) {
					// 月租
					parkDataResult = payCommonFacade.monthPayFailNotice(szOrderId);
				} else if (orderTypeInt == OrderParameter.OrderTypeEnum.CUSTOMER_PAYMENT_TYPE.getType()) {
					// 代缴
					parkDataResult = payCommonFacade.teneratPayFailNotice(szOrderId);
				} else if (orderTypeInt == OrderParameter.OrderTypeEnum.RESERVER_TYPE.getType()) {
					// 预定
					parkDataResult = payCommonFacade.reserverPayFailNotice(szOrderId);
				} else if (orderTypeInt == OrderParameter.OrderTypeEnum.TEMP_PAY_TYPE.getType()) {
					// 临停
					parkDataResult = payCommonFacade.tempPayFailNotice(szOrderId);
				}
			}
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createAnswerMsg(parkDataResult.getData());
			return answerMsg;
		} catch (RchlValidatorException e) {

			e.handle();
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.VALIDATION_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText() + "/" + e.getExceptionMessage().getDetailText());
			return answerMsg;
		} catch (AdapterException e) {
			exceptionHandle.handle(e);// 接口异常
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.BUSINESS_ERROR.getValue(),
					e.getExceptionMessage().getSimpleText());
			return answerMsg;
		} catch (Exception e) {
			exceptionHandle.handle(e);
			AnswerMsg answerMsg = AnswerMsgFactoryImpl.createErrorAnserMsg(ResponseCodeEnum.SYSTEM_ERROR.getValue(),
					"系统异常，请稍后再试");
			return answerMsg;
		}

	}

}
